/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------------
 * PieAgentsChart.java
 * ------------------
 * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   ;
 *
 * Changes
 * -------
 * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
 *               the JFreeChart Developer Guide (DG);
 *
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.charts;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.SortOrder;
import sk.lieskove301.jianghongtiao.liaad.prisoner.agent.Agent;
import sk.lieskove301.jianghongtiao.liaad.prisoner.gene.GeneticInfo;
import sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.AgentsTimelineUnit;
import sk.lieskove301.jianghongtiao.liaad.prisoner.statistics.CommonStatistics;

/**
 * A simple demonstration application showing how to create a pie chart using
 * data from a {@link DefaultPieDataset}.
 */
public final class PieAgentsChart extends ApplicationFrame {

    private String title;
    private Map<String, AgentsTimelineUnit> agents;
    private JFreeChart chart;
    private SelectStat stat;

    /**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public PieAgentsChart(String title, Map<String, 
            AgentsTimelineUnit> agents, SelectStat.FUNC_TYPE statF) {
        super(title);
        this.title = title;
        this.agents = agents;
        this.stat = SelectStat.getInstance(statF);
        setContentPane(createPanel());
    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    private PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        double sum = 0.0;
        for (Iterator<String> it = agents.keySet().iterator(); it.hasNext();) {
            String geneticInfo = it.next();
            if(agents.containsKey(geneticInfo)){
                AgentsTimelineUnit atu = agents.get(geneticInfo);
                sum += stat.stat(atu);
            } else {
                System.out.println("Does not contains: "+geneticInfo.toString());
            }
        }
        double min = (sum/100.0)*5;
        double other = 0.0;
        for (Iterator<String> it = agents.keySet().iterator(); it.hasNext();) {
            String geneticInfo = it.next();
            if(agents.containsKey(geneticInfo)){
                AgentsTimelineUnit atu = agents.get(geneticInfo);
                if(min < stat.stat(atu)){
                    dataset.setValue(geneticInfo.toString(), stat.stat(atu));
                } else {
                    other += stat.stat(atu);
                }
                
            } else {
                System.out.println("Does not contains: "+geneticInfo.toString());
            }
        }
        dataset.setValue("Other: ", other);
        dataset.sortByValues(SortOrder.ASCENDING);
        return dataset;
    }

    /**
     * Creates a chart.
     *
     * @param dataset  the dataset.
     *
     * @return A chart.
     */
    private JFreeChart createChart(PieDataset dataset) {

        chart = ChartFactory.createPieChart3D(
            this.getTitle(),  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionOutlinesVisible(false);
        plot.setNoDataMessage("No data available");
        chart.getPlot().setForegroundAlpha(0.5f);

        return chart;

    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public JPanel createPanel() {
        chart = createChart(createDataset());
        try {
            if(false)ChartUtilities.saveChartAsPNG(new File(title+".png"), chart, 3000, 5000);
        } catch (IOException ex) {
            Logger.getLogger(AbstractChart.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        return new ChartPanel(chart);
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public void print() {
        pack();
        RefineryUtilities.centerFrameOnScreen(this);
        setVisible(true);
    }

}
